package com.excellent.mvvm.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.viewbinding.ViewBinding;

import com.excellent.mvvm.R;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author: david
 * @date: 2020/12/28
 * @des:
 */
public class BaseBootSheetDialogFragment<T extends ViewBinding> extends BottomSheetDialogFragment {



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.AppBottomSheet);
    }

    protected T binding;

    private View view;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        Type superclass = getClass().getGenericSuperclass();
        Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class,ViewGroup.class,boolean.class);
            binding = (T) method.invoke(null, getLayoutInflater(),container,false);
        } catch (NoSuchMethodException | IllegalAccessException| InvocationTargetException e) {
            e.printStackTrace();
        }
        initView();
        initData();
        initListener();
        return binding.getRoot();
    }

    public void initListener() {

    }

    public void initData() {

    }

    public void initView() {
    }




    public void CanceledOnTouchOutside(){
        getDialog().setCanceledOnTouchOutside(false);
    }


    @Override
    public void dismiss() {
        if (getActivity()==null || getActivity().isFinishing()){
            return;
        }
        if (getDialog()!=null && getDialog().isShowing()){
            super.dismiss();
        }
    }
}
